<template>
  <div class="table_continer">
    <div style="position: absolute;top: -50px;right: 0px">
      <right-toolbar @queryTable="updateFlag" :columns="columns" />
    </div>
    <div v-if="tableflag">
      <!-- 列表 ------------------------------------------------------------------------------------------------------->
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column prop="equipmentName" label="序号" width="100">
          <template slot-scope="scope">
            <div>{{ scope.$index + 1 }}</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[0].visible" prop="materialNum" label="物料编号" min-width="160"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <el-link @click="handledetailed(scope.row.consumableBaseId)" type="primary">{{ scope.row.materialNum || '-'
            }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[1].visible" prop="materialName" label="物料名称" min-width="120" />
        <el-table-column v-if="columns[2].visible" prop="typeName" label="类别" min-width="120" />
        <el-table-column v-if="columns[3].visible" prop="model" label="规格型号" min-width="120">
          <template slot-scope="scope">
            <div> {{ scope.row.model || '-' }} </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[4].visible" prop="unitName" label="单位" min-width="120" />
        <el-table-column v-if="columns[5].visible" prop="kind" label="类型" min-width="120">
          <template slot-scope="scope">
            <dict-tags :options="SparePartsAndConsumables" :value="scope.row.kind" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[6].visible" prop="supplierName" label="供应商" width="120" show-overflow-tooltip />
        <el-table-column v-if="columns[7].visible" prop="equipmentStatus" label="库存预警值" min-width="120">
          <template slot-scope="scope">
            <div> {{ scope.row.lowerLimit }} ~ {{ scope.row.highLimit }} </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[8].visible" prop="purchaseCycle" label="采购周期（天）" min-width="150">
          <template slot-scope="scope">
            <div> {{ scope.row.purchaseCycle }} 天</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[9].visible" prop="replaceCycle" label="更换周期（天）" min-width="150">
          <template slot-scope="scope">
            <div> {{ scope.row.replaceCycle }} 天</div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[10].visible" label="适用设备" min-width="150" show-overflow-tooltip>
          <template slot-scope="{row}">
            <div> {{ row.equipmentNames || '-' }} </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[11].visible" prop="status" label="状态" min-width="150">
          <template slot-scope="scope">
            <dict-tag :options="statusFlag" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column v-if="columns[12].visible" prop="remark" label="备注" width="150" show-overflow-tooltip>
          <template slot-scope="{row}">
            <div> {{ row.remark || '-' }} </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns[13].visible" label="创建部门" align="center" min-width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.deptName || '-' }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns[14].visible" label="创建人" align="center" min-width="180" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.createBy || '-' }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="280">
          <template slot-scope="scope">
            <!-- 此处ID字段特别注意 ------------------------------------------------------------------------------------>
            <el-button v-hasPermi="['management_left_update']" type="primary" size="small" icon="el-icon-edit"
              @click="handleEdit(scope.row.consumableBaseId)">
              编辑</el-button>
            <el-button v-hasPermi="['management_left_dis']" v-show="scope.row.status == 0" icon="el-icon-lock"
              type="danger" size="small" @click="handleAlter(scope.row.consumableBaseId, scope.row.status)">禁用
            </el-button>
            <el-button v-hasPermi="['management_left_dis']" v-show="scope.row.status == 1" icon="el-icon-unlock"
              type="success" size="small" @click="handleAlter(scope.row.consumableBaseId, scope.row.status)">启用
            </el-button>
            <el-button size="small" type="primary" icon="el-icon-view" @click="handleSelectImage(scope.row.image)">图片
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- ------------------------------------------------------------------------------------------------------------>
    </div>
    <Dialog-image v-if="ImageOpen" :src="imageSrc" />

    <!-- --------------------------------分页器---------------------------------------------------------- -->
    <div class="table_continer_pagination">
      <el-pagination :current-page="pageParams.pageNum" background :page-sizes="[10, 20, 50, 100]"
        :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
/** 拉取列表远程方法 ---------------------------------------------------------------------------------------------------------- */
import { list, status } from '@/api/deviceConsumable/management'
import { getcolumnslist, Updatecolumns } from '@/api/menu.js'
/** ------------------------------------------------------------------------------------------------------------------------- */
import { pageCount } from '@/utils/pagination'
/** 查询参数处理 ---------------------------------------------------------------------------------------------------------- */
function processQueryParams (queryParams, pageParams) {
  const params = Object.assign({}, queryParams, pageParams)
  return params
}
/** ------------------------------------------------------------------------------------------------------------------------- */
export default {
  name: 'PaginationTable',
  data () {
    return {
      columns: [
        { key: 0, label: `物料编号`, visible: true, disabled: true },
        { key: 1, label: `物料名称`, visible: true, disabled: true },
        { key: 2, label: `类别`, visible: true },
        { key: 3, label: `规格型号`, visible: true },
        { key: 4, label: `单位`, visible: true, },
        { key: 5, label: `类型`, visible: true, },
        { key: 6, label: `供应商`, visible: true },
        { key: 7, label: `库存预警值`, visible: true, },
        { key: 8, label: `采购周期（天）`, visible: true, },
        { key: 9, label: `更换周期（天）`, visible: true, },
        { key: 10, label: `适用设备`, visible: true },
        { key: 11, label: `状态`, visible: true },
        { key: 12, label: `备注`, visible: true },
        { key: 13, label: `创建部门`, visible: true },
        { key: 14, label: `创建人`, visible: true },
      ],
      pagination: {
        total: 1,
        pageCount: 1
      },
      pageParams: {
        pageNum: 1,
        pageSize: 10,
        /** 列表默认排序字段 ---------------------------------------------------------------------------------------------------------- */
        orderByColumn: 'consumableBaseId',
        /** ------------------------------------------------------------------------------------------------------------------------- */
        isAsc: 'desc'
      },
      queryParams: null,
      tableData: [],
      multipleSelection: [],
      ImageOpen: false,
      imageSrc: undefined,
      tableflag: true
    }
  },
  mounted () {
    this.refreshList();
    this.getcolumns();
  },
  watch: {
    columns: {//监听动态列修改调用更新方法
      handler (val) {
        this.tableflag = false
        this.updateFlag()
      },
      deep: true
    }
  },
  methods: {
    /**更新动态列数据 */
    async updateFlag () {
      let params = {
        viewKey: 'consumable_base',
        columnFlag: JSON.stringify(this.columns)
      }
      await Updatecolumns(params)
      this.tableflag = true
    },
    /**查询动态列数据 */
    async getcolumns () {
      let { data: data } = await getcolumnslist({ 'viewKey': 'consumable_base' })
      console.log(data);
      let str = ''
      if (data.length !== 0) {
        data.map(item => {
          if (item.viewKey === 'consumable_base') {
            // str = JSON.parse(eval('(' + item.columnFlag + ')'))
            str = JSON.parse(item.columnFlag)
            return false
          }
        })
        this.columns = str
      }
    },
    refreshList () {
      const params = processQueryParams(this.queryParams, this.pageParams)
      console.log('查询参数', params);
      // --------consolelog('查询参数', params)
      list(params).then((res) => {
        console.log('获取列表', res);
        // --------consolelog('获取列表', res)
        this.pagination.total = res.total
        this.pagination.pageCount = pageCount(this.pagination.total, this.pageParams.pageSize)
        this.tableData = res.data
      })
    },
    handleQuery (queryParams) {
      this.queryParams = queryParams
      this.refreshList()
    },
    handleSizeChange (value) {
      this.pageParams.pageSize = value
      this.refreshList()
    },
    handleCurrentChange (value) {
      this.pageParams.pageNum = value
      this.refreshList()
    },
    handleSelectionChange (value) {
      // console.log('handleSelectionChange', value)
    },
    // 点击编号跳转基值详情-----------------------------
    handledetailed (id) {
      this.$emit('click_create_reception', id)
    },
    /** 传递点击【编辑】事件 ---------------------------------------------------------------------------------------------------------- */
    handleEdit (id) {
      this.$emit('click_edit', id)
    },
    // 禁用----启用事件------------------------------------------------------------------------------------------------------------------
    handleAlter (consumableBaseId, type) {
      const rule = {
        consumableBaseId: consumableBaseId,
        status: type == 0 ? 1 : 0
      }
      status(rule).then(() => {
        if (type == 0) {
          this.$message.error('禁用成功');
        } else {
          this.$message.success('启用成功');
        }
        this.refreshList()
      })
    },
    handleSelectImage (src) {
      this.imageSrc = src
      this.ImageOpen = true
    }
  }
}
</script>
<style lang="scss" scoped>
.table_continer {
  border-radius: 5px;
  position: relative;

  &_pagination {
    background-color: white;
    padding: 10px;
    margin-bottom: 10px;
  }
}
</style>
